//
//  YXNetworkTool.swift
//  YXweibo
//
//  Created by AS on 5/12/16.
//  Copyright © 2016 AS. All rights reserved.
//

import UIKit
import AFNetworking


// 请求方法枚举
enum RequestMethod:String
{
    case GET = "GET"
    case POST = "POST"
}
class YXNetworkTool: NSObject {

    
    // 单例
    static let sharedInstance:YXNetworkTool = YXNetworkTool()
    
    // 属性AFHttpSessionManager
    private let afnManager : AFHTTPSessionManager = {
        let afn = AFHTTPSessionManager(baseURL: NSURL(string: "https://api.weibo.com/"))
         // 给响应的序列化器多添加支持的类型
        afn.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return afn
    }()
    //   func requestGET(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        //        afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        //    }
        //
        //    func requestPOST(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
        //        afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        //    }
        
        // 如果直接使用AFN的GET和POST方法,如果AFN改变了方法名或参数改变了,哪项目中直接使用AFN.GET和POST的地方都需要改变
        // 自己封装GET 和 POST方法,项目中要使用GET和POST就用我们封装好的,到时如果如果AFN改变了方法名或参数改变了,只需要改我们封装的方法
        /// 封装AFN的GET和POST,通过参数来确定使用GET还是POST
    
    func request(method:RequestMethod,URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        if method == RequestMethod.GET {
            afnManager.GET(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else
        {
            afnManager.POST(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
}
